import dayjs from "dayjs";
import type { TDate } from "../../miniprogram_npm/tdesign-miniprogram/calendar";
import request from "../../utils/http";
import { DateService } from "../../utils/type";

// pages/schedule/schedule.ts
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		actDate: {
			timestamp: 0,
			str: ''
		},
		toDay: new Date().getTime(),
		serviceDateList: <string[]>[],
		minDate: dayjs().subtract(1, "month").valueOf(),
		getServiceDateListStatus: <'await' | 'ing' | 'end'>'await',
		formatCalendarDate: <any>null,
		dateServiceList: <DateService[]>[]
	},

	async getDateServiceList() {
		try {
			const dateServiceList = await request({
				url: '/api/customers/order/date/service',
				method: 'POST',
				data: {
					date: this.data.actDate.str
				},
				contentTypeForm: true,
				errToast: true
			});
			this.setData({dateServiceList});
		} catch (error) {
			
		}
	},

	async getServiceDateList() {
		this.setData({getServiceDateListStatus: 'ing'});
		let serviceDateList: string[] = [];
		try {
			serviceDateList = await request({
				url: '/api/customers/order/service/date',
				method: 'POST'
			});
			this.setData({serviceDateList});
		} catch (error) {
			
		}
		let formatCalendarDate = null;
		if (serviceDateList.length > 0) {
			formatCalendarDate = (day: TDate) => {
				const date = dayjs(day.date).format("YYYY-MM-DD");
				if (serviceDateList.includes(date)) {
					day.prefix = '●';
					day.className = 'service-date';
				}
				return day;
			}
		}
		this.setData({
			getServiceDateListStatus: 'end',
			formatCalendarDate
		});
	},

	handleSelect(e: WechatMiniprogram.CustomEvent) {
		const { value } = e.detail;
		const actDate = this.data.actDate;
		actDate.timestamp = value;
		actDate.str = dayjs(value).format('YYYY-MM-DD');
		this.setData({actDate});
		this.getDateServiceList();
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad() {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.getServiceDateList();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})